/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.dataInterface;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.db.DynamicDataSource;
import com.thinkgem.jeesite.common.utils.CustomPasswordEncoder;
import com.thinkgem.jeesite.common.utils.PropertiesLoader;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.acq.entity.dataview.AcqDataview;
import com.thinkgem.jeesite.modules.acq.entity.fammer.AcqFarmer;
import com.thinkgem.jeesite.modules.acq.entity.userarea.AcqUserArea;
import com.thinkgem.jeesite.modules.acq.service.breedinfo.AcqBreedInfoService;
import com.thinkgem.jeesite.modules.acq.service.breedtype.AcqBreedTypeService;
import com.thinkgem.jeesite.modules.acq.service.dataview.AcqDataviewService;
import com.thinkgem.jeesite.modules.acq.service.dxdan.AcqDxdanService;
import com.thinkgem.jeesite.modules.acq.service.fammer.AcqFarmerService;
import com.thinkgem.jeesite.modules.acq.service.news.AcqNewsService;
import com.thinkgem.jeesite.modules.acq.service.plantinfo.AcqPlantInfoService;
import com.thinkgem.jeesite.modules.acq.service.planttype.AcqPlantTypeService;
import com.thinkgem.jeesite.modules.acq.service.userarea.AcqUserAreaService;
import com.thinkgem.jeesite.modules.sys.dao.AreaDao;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.AreaService;
import com.thinkgem.jeesite.modules.sys.service.DictService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;


/**
 * 公用接口
 * @author lj
 * @version 2018-3-20
 */
@Controller
@RequestMapping(value = "")
public class CommonController extends BaseController {
	@Autowired AreaService areaService;
	@Autowired DictService dictService;
	@Autowired AcqBreedTypeService breedService;
	@Autowired AcqPlantTypeService plantService;
	@Autowired AcqFarmerService fammerService;
	@Autowired AcqUserAreaService userAreaService;
	@Autowired UserDao userDao;
	@Autowired AcqNewsService acqNewsService;
	@Autowired AcqBreedInfoService acqBreedInfoService;
	@Autowired AcqPlantInfoService acqPlantInfoService;
	@Autowired AcqDxdanService acqDxdanService;
	@Autowired SystemService sysService;
	@Autowired AcqDataviewService acqDataviewService;
	@Autowired AreaDao areaDao;
	private static PropertiesLoader loader = new PropertiesLoader("jeesite.properties");
	// 行政区域json查询
	@RequestMapping(value = "areajson")
	@ResponseBody
	public Map<String,Object> areajson(String uid,HttpServletRequest request, HttpServletResponse response, String id) {
		return areaService.areajson(uid);
	}
	
	// 种植面积单位
		@RequestMapping(value = "plantAreaJson")
		@ResponseBody
		public Map<String,Object> plantAreaJson(HttpServletRequest request, HttpServletResponse response) {

			return dictService.plantAreaJson();
		}
		
		// 产量单位
		@RequestMapping(value = "returnUnitJson")
		@ResponseBody
		public Map<String,Object> returnUnitJson(HttpServletRequest request, HttpServletResponse response) {

			return dictService.returnUnitJson();
		}	
	
	// 产品等级json查询
	@RequestMapping(value = "leveljson")
	@ResponseBody
	public Map<String,Object> leveljson(HttpServletRequest request, HttpServletResponse response) {

		return dictService.leveljson();
	}
	
	// 养殖品类json查询
	@RequestMapping(value = "breedjson")
	@ResponseBody
	public Map<String,Object> breedjson(HttpServletRequest request, HttpServletResponse response) {

		return breedService.breedjson();
	}
	
	// 种植品类json查询
	@RequestMapping(value = "plantjson")
	@ResponseBody
	public Map<String,Object> plantjson(HttpServletRequest request, HttpServletResponse response) {

		return plantService.plantjson();
	}
	// 农户json查询
	@RequestMapping(value = "fammerjson")
	@ResponseBody
	public Map<String,Object> fammerjson(AcqFarmer fammer,HttpServletRequest request, HttpServletResponse response) {
		
		return fammerService.fammerjson(fammer);
	}
	
	
	// 农户种养殖产品查询
	@RequestMapping(value = "infojson")
	@ResponseBody
	public Map<String,Object> infojson(AcqFarmer fammer,HttpServletRequest request, HttpServletResponse response) {
		
		return fammerService.infojson(fammer);
	}
	
	// 农户列表
	@RequestMapping(value = "fammers")
	@ResponseBody
	public Map<String,Object> fammers(AcqFarmer fammer,HttpServletRequest request, HttpServletResponse response) {
		
		return fammerService.fammers(fammer);
	}
	
	/*
	 * 手机APP登录
	 **/
	@RequestMapping(value = "/mlogin", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> mlogin(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> result = new HashMap<String, Object>();
		CustomPasswordEncoder encoder = new CustomPasswordEncoder();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		try {
			// 数据库用户
			User dbuser = new User();
			dbuser.setLoginName(username);
			dbuser = userDao.getByLoginName(dbuser);
			// 验证密码
			boolean flag = encoder.matches(password, dbuser.getPassword());
			//查询用户机构权限区域
			if(flag){
				Map<String,Object> area = userDao.findOfficeArea(dbuser.getId());
				result.put("area",area);
			}
		
			result.put("user", dbuser);
			
			result.put("status", flag);
			result.put("message", flag ? "登录成功" : "登录失败,用户名或密码错误");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", false);
			result.put("message", "登录失败，用户不存在");
			return result;
		}

	}
	// 录入员负责村子json查询
	@RequestMapping(value = "getDutyArea")
	@ResponseBody
	public Map<String,Object> getDutyArea(AcqUserArea acqUserArea,HttpServletRequest request, HttpServletResponse response) {
		
		return userAreaService.getDutyArea(acqUserArea);
	}

	// 录入员负责村子json查询
	@RequestMapping(value = "news")
	@ResponseBody
	public Map<String,Object> news(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		result = acqNewsService.news();
		return result;
	}
	@RequestMapping(value = "infopie")
	@ResponseBody
	public Map<String,Object> pie(String areaId,HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("breedpie", acqBreedInfoService.pie(areaId)); 
		result.put("plantpie", acqPlantInfoService.pie(areaId)); 
		result.put("breedline", acqBreedInfoService.infoline(areaId)); 
		result.put("plantline", acqPlantInfoService.infoline(areaId)); 
		result.put("dxpie", acqDxdanService.dxpie(areaId));
		result.put("dxline", acqDxdanService.dxline(areaId));
		result.put("fammerpie", fammerService.farmerpie(areaId));
		result.put("farmerline",fammerService.farmerline(areaId)); 

		return result;
	}
	
	@RequestMapping(value = "stockLine")
	@ResponseBody
	public Map<String,Object> stockLine(String areaId,HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		//产销统计双柱图
		result.put("stockLine", acqBreedInfoService.stockLine(areaId)); 

		return result;
	}
	
	
	//修改密码
	@RequestMapping(value = "changePassword")
	@ResponseBody
	public Map<String,Object> changePassword(String id,String oldpass,String newpass,HttpServletRequest request, HttpServletResponse response) {
		
		return sysService.changePassword(id,oldpass,newpass);
	}
	
	//根据区域Id查询下级区域
	@RequestMapping(value = "findChildJson")
	@ResponseBody
	public Map<String,Object> findChildJson(String areaId,HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("data",  areaService.findChildJson(areaId));
		return result;
	}
	
	//返回当前版本号
	@RequestMapping(value = "getVersion")
	@ResponseBody
	public Map<String,Object> getVersion(HttpServletRequest request, HttpServletResponse response) {		
		Map<String,Object> result = new HashMap<String,Object>();
		String version = loader.getProperty("appversion");
		String appupdateinfo = loader.getProperty("appupdateinfo");
		result.put("version", version);
		result.put("info", appupdateinfo);
		return result;
	}
	
	//大屏获取村信息接口
	@RequestMapping(value = "getInfoByAreaId")
	@ResponseBody
	public Map<String,Object> getInfoByAreaId(AcqDataview acqDataview,HttpServletRequest request, HttpServletResponse response) {		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("data", acqDataviewService.getInfoByAreaId(acqDataview));
		return result;
	}
		//最底层区域JSON
	@RequestMapping(value = "lastAreaJson")
	@ResponseBody
	public Map<String,Object> lastAreaJson(HttpServletRequest request, HttpServletResponse response) {		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("data", areaDao.lastAreaJson());
		return result;
	}
	
	//old
	@RequestMapping(value = "getData")
	@ResponseBody
	public Map<String,Object> getData(HttpServletRequest request, HttpServletResponse response) {
		String z = request.getParameter("z");
		String k = request.getParameter("k");
		// 切换数据源dataSource2，默认数据源dataSource
		DynamicDataSource.setCurrentLookupKey("dataSource2");
		Map<String,Object> result = dictService.getData(z,k);
		DynamicDataSource.setCurrentLookupKey("dataSource");
		return result;
	}
	
	//trend
	@RequestMapping(value = "trend")
	@ResponseBody
	public Map<String,Object> trend(HttpServletRequest request, HttpServletResponse response) {
		String z = request.getParameter("z");
		String k = request.getParameter("k");
		Integer limit = Integer.valueOf(request.getParameter("limit"));
		// 切换数据源dataSource2，默认数据源dataSource
		DynamicDataSource.setCurrentLookupKey("dataSource2");
		Map<String,Object> result = dictService.trend(limit,z,k);
		DynamicDataSource.setCurrentLookupKey("dataSource");
		return result;
	}
	
}
